package cn.jiyun.service.impl;


import cn.jiyun.mapper.DevMapper;
import cn.jiyun.mapper.ProjectMapper;
import cn.jiyun.page.PageResult;
import cn.jiyun.page.QueryPageBean;
import cn.jiyun.pojo.Dev;
import cn.jiyun.pojo.Project;
import cn.jiyun.service.DevService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DevServiceImpl implements DevService {
    @Autowired
    private DevMapper devMapper;
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public int add(int[] ids,Dev dev) {
        dev.setUpdatetime(new Date());
        int add = devMapper.add(dev);
        Integer did = dev.getDid();
        for (int i = 0; i < ids.length; i++) {
            int id = ids[i];
            projectMapper.addDid(did,id);
        }
         return  add;
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        List<Map> list = devMapper.findAll((queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize(), queryPageBean.getPageSize(),queryPageBean.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        Long pageSize =devMapper.getCount();
        pageResult.setTotal(pageSize);
        return new PageResult(pageResult.getTotal(),pageResult.getRows());
    }

    @Override
    public int update(int[] ids,Dev dev) {
        dev.setUpdatetime(new Date());
        int add = devMapper.update(dev);
        Integer did = dev.getDid();
        devMapper.clean(did);
        for (int i = 0; i < ids.length; i++) {
            int id = ids[i];
            projectMapper.update2(did,id);
        }
        return  add;
    }

    @Override
    public int delete(Integer did) {
        Project byPro = devMapper.findByPro(did);
        byPro.setDid("0");
        projectMapper.update(byPro);
        return devMapper.delete(did);
    }

    @Override
    public Dev findById(Integer did) {
        return  devMapper.findById(did);

    }

    @Override
    public List<Project> findProjectList(Integer did) {
        return devMapper.findProjectList(did);
    }

    @Override
    public List<Project> findProjectList2() {
        return devMapper.findProjectList2();
    }

    @Override
    public List<Dev> findName() {
        return devMapper.findName();
    }
}
